package ModelLayer;
import java.util.ArrayList;

/**
 * The LoanCollections is a container for the Class Loan, it has an ArrayList and a private constructor
 * so that we can use a single instance of it.
 * 
 * @author Gruppe A 
 * @version 1.0
 */
public class LoanCollections
{
    // instance variables - replace the example below with your own
    private ArrayList<Loan> myLoans;
    private static LoanCollections instance;

    /**
     * Constructor class LoanCollections
     */
    private LoanCollections()
    {
        // initialise instance variables
        myLoans = new ArrayList<Loan>();
    }

    /**
     * Method for checking if an instance of the class allready exists, if it doesn't one is created.
     * @return LoanCollections instance.
     */
    public static LoanCollections getInstance()
    {
        if(instance == null)
        {
            instance = new LoanCollections();
        }
        return instance;
    }

    /**
     * A mutator method for adding objects of the type Loan to the ArrayList.
     * @param Loan loan.
     */
    public void addLoan(Loan loan)
    {
        myLoans.add(loan);
    }

    /**
     * A boolean method for getting the status of a loan. It takes an id number to identify the loan and
     * then returns true or false
     * @param long id
     * @return boolean l.getStatus
     */
    public boolean getStatus(long id)
    {
        Loan l = findLoan(id);
        return l.getStatus();
    }

    /**
     * An accessor method for finding specific objects in the ArrayList, it takes a String variable as a parameter. And 
     * returns a specific object of DVD
     * @param String title.
     * @return DVD specific dvd.
     */
    public Loan findLoan(long id)
    {
        int index = 0;
        boolean searching = true;
        while(index < myLoans.size() && searching)
        {
            Loan file = myLoans.get(index);
            if(file.getId()== id)
            {
                searching = false;         
            }
            else
            {
                index++;
            }
        }
        if(!searching)
        {
            return myLoans.get(index);
        }
        else
        {
            return null;
        }
    }

    /**
     * A mutator method for adding a copy.
     * @param DVD d
     * @param long serialnr
     * @param long id
     */
    public void addCopy(DVD d, long serialnr, long id)
    {
        Loan l = findLoan(id);
        Copy c = d.findCopy(serialnr);
        l.dvdLoan(c);
    }

    /**
     * A mutator method for reomving an object of DVD from the ArrayList.
     * @param DVD dvd.
     */
    public void returnLoan(Loan loan)
    {
        loan.received();
    }

    /**
     * A mutator method for updating the attributes of a specific DVD object. Takes different parameters and substitutes
     * them for the original data. Thus updating the object.
     * @param DVD dvd
     * @param String title
     * @param String director
     */
    public void updateLoan(Loan loan, int deliveryDateMonth, int deliveryDateDay, String home)
    {
        if(loan != null)
        {
            if(deliveryDateMonth != 0)
            {
                loan.setDeliveryDateMonth(deliveryDateMonth);
            }
            if(deliveryDateDay != 0)
            {
                loan.setDeliveryDateDay(deliveryDateDay);
            }
            if(home.equals("yes"))
            {
                loan.received();
            }
            if(home.equals("no"))
            {
                loan.loanedOut();
            }
        }

    }

    /**
     * A method that creates a new arraylist containing the al the information regarding the current loans.
     * @return ArrayList listAll
     */
    public ArrayList<String> listAllLoans()
    {
        ArrayList<String> listAll = new ArrayList<String>();
        for(Loan l: myLoans)
        {
            listAll.add("Delivery Date: "+Integer.toString(l.getDeliveryDateMonth())+"-"+Integer.toString(l.getDeliveryDateDay()));
            listAll.add("Loaned to: "+l.getPerson());
            listAll.add("\n");
            listAll.addAll(l.getDVD());
            listAll.add("\n");
        }
        return listAll;
    }
}